package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 上午11:55
 */
@Entity
@Table(name = "major")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update major set major_is_deleted = true where major_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "major_is_deleted = false")
@ApiModel("专业")
@JsonIgnoreProperties(value={"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Major extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId",
                      parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "MAJOR")})
    @Column(name = "major_id")
    @ApiModelProperty(value = "major的id", position = 1)
    private String majorId;

    @Column(name = "major_name")
    @ApiModelProperty(value = "专业名字", position = 2)
    private String majorName;

    @ManyToOne(targetEntity = Institution.class, cascade = CascadeType.REFRESH, fetch = FetchType.LAZY)
    @JoinColumn(name = "major_institution_id", referencedColumnName = "institution_id")
    @ApiModelProperty(value = "专业学院", position = 3)
    private Institution institution;

    @Column(name = "major_is_forbidden")
    @ApiModelProperty(value = "专业是否被禁用", position = 4)
    private boolean majorIsForbidden = false;

    @Column(name = "major_is_deleted")
    @ApiModelProperty(value = "专业是否被删除", position = 5)
    private boolean majorIsDeleted = false;

    @OneToMany(mappedBy = "major", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Student> students = new ArrayList<>();

    @OneToMany(mappedBy = "major", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Teacher> teachers = new ArrayList<>();

    @OneToMany(mappedBy = "major", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<TeacherCourse> teacherCourses = new ArrayList<>();

    @OneToMany(mappedBy = "major", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<StudentCourse> studentCourses = new ArrayList<>();

    public Major(String majorId, String majorName, Institution institution, boolean majorIsForbidden, boolean majorIsDeleted, List<Student> students, List<Teacher> teachers, List<TeacherCourse> teacherCourses, List<StudentCourse> studentCourses) {
        this.majorId = majorId;
        this.majorName = majorName;
        this.institution = institution;
        this.majorIsForbidden = majorIsForbidden;
        this.majorIsDeleted = majorIsDeleted;
        this.students = students;
        this.teachers = teachers;
        this.teacherCourses = teacherCourses;
        this.studentCourses = studentCourses;
    }

    public Major() {
    }

    public static MajorBuilder builder() {
        return new MajorBuilder();
    }

    public String getMajorId() {
        return this.majorId;
    }

    public String getMajorName() {
        return this.majorName;
    }

    public Institution getInstitution() {
        return this.institution;
    }

    public boolean isMajorIsForbidden() {
        return this.majorIsForbidden;
    }

    public boolean isMajorIsDeleted() {
        return this.majorIsDeleted;
    }

    public List<Student> getStudents() {
        return this.students;
    }

    public List<Teacher> getTeachers() {
        return this.teachers;
    }

    public List<TeacherCourse> getTeacherCourses() {
        return this.teacherCourses;
    }

    public List<StudentCourse> getStudentCourses() {
        return this.studentCourses;
    }

    public void setMajorId(String majorId) {
        this.majorId = majorId;
    }

    public void setMajorName(String majorName) {
        this.majorName = majorName;
    }

    public void setInstitution(Institution institution) {
        this.institution = institution;
    }

    public void setMajorIsForbidden(boolean majorIsForbidden) {
        this.majorIsForbidden = majorIsForbidden;
    }

    public void setMajorIsDeleted(boolean majorIsDeleted) {
        this.majorIsDeleted = majorIsDeleted;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public void setTeachers(List<Teacher> teachers) {
        this.teachers = teachers;
    }

    public void setTeacherCourses(List<TeacherCourse> teacherCourses) {
        this.teacherCourses = teacherCourses;
    }

    public void setStudentCourses(List<StudentCourse> studentCourses) {
        this.studentCourses = studentCourses;
    }

    public static class MajorBuilder {
        private String majorId;
        private String majorName;
        private Institution institution;
        private boolean majorIsForbidden;
        private boolean majorIsDeleted;
        private List<Student> students;
        private List<Teacher> teachers;
        private List<TeacherCourse> teacherCourses;
        private List<StudentCourse> studentCourses;

        MajorBuilder() {
        }

        public MajorBuilder majorId(String majorId) {
            this.majorId = majorId;
            return this;
        }

        public MajorBuilder majorName(String majorName) {
            this.majorName = majorName;
            return this;
        }

        public MajorBuilder institution(Institution institution) {
            this.institution = institution;
            return this;
        }

        public MajorBuilder majorIsForbidden(boolean majorIsForbidden) {
            this.majorIsForbidden = majorIsForbidden;
            return this;
        }

        public MajorBuilder majorIsDeleted(boolean majorIsDeleted) {
            this.majorIsDeleted = majorIsDeleted;
            return this;
        }

        public MajorBuilder students(List<Student> students) {
            this.students = students;
            return this;
        }

        public MajorBuilder teachers(List<Teacher> teachers) {
            this.teachers = teachers;
            return this;
        }

        public MajorBuilder teacherCourses(List<TeacherCourse> teacherCourses) {
            this.teacherCourses = teacherCourses;
            return this;
        }

        public MajorBuilder studentCourses(List<StudentCourse> studentCourses) {
            this.studentCourses = studentCourses;
            return this;
        }

        public Major build() {
            return new Major(majorId, majorName, institution, majorIsForbidden, majorIsDeleted, students, teachers, teacherCourses, studentCourses);
        }

        public String toString() {
            return "Major.MajorBuilder(majorId=" + this.majorId + ", majorName=" + this.majorName + ", institution=" + this.institution + ", majorIsForbidden=" + this.majorIsForbidden + ", majorIsDeleted=" + this.majorIsDeleted + ", students=" + this.students + ", teachers=" + this.teachers + ", teacherCourses=" + this.teacherCourses + ", studentCourses=" + this.studentCourses + ")";
        }
    }
}
